package com.baimifan.renovation.homefrag;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baimifan.renovation.R;
import com.baimifan.renovation.adapter.FragmentAdapter;
import com.baimifan.renovation.manager.FindClassifyManager;
import com.flyco.tablayout.SlidingTabLayout;

import org.xutils.common.util.DensityUtil;

import java.lang.ref.WeakReference;


/**
 * 首页-发现.
 */
public class FindFragment extends Fragment {
    private Float w, h, widthPixels, heightPixels;
    private SlidingTabLayout find_tab;
    private ViewPager find_viewpager;
    private FragmentAdapter adapter;
    private FindClassifyManager findClassifyManager;
    private MyHandler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        init(view);
        getDisplay();
        setLayoutParams();
        find_tab.setBackgroundColor(Color.parseColor("#FF5F2F"));
        find_tab.setTextSelectColor(Color.WHITE);
        find_tab.setTextUnselectColor(Color.WHITE);
        find_tab.setTextsize(17);
        find_tab.setIndicatorHeight(4);
        find_tab.setIndicatorWidth(49);
        find_tab.setTabSpaceEqual(true);
        findClassifyManager.findclassify();
        return view;
    }

    private void init (View view) {
        find_tab = (SlidingTabLayout) view.findViewById(R.id.find_tab);
        find_viewpager = (ViewPager) view.findViewById(R.id.find_viewpager);
        handler = new MyHandler(this);
        findClassifyManager = new FindClassifyManager(handler);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void setLayoutParams () {
        LinearLayout.LayoutParams params_find_tab = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(h*44));
        find_tab.setLayoutParams(params_find_tab);
    }

    private class MyHandler extends Handler {
        WeakReference<FindFragment> mfind;

        public MyHandler (FindFragment findFragment) {
            mfind = new WeakReference<FindFragment>(findFragment);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            FindFragment find = mfind.get();
            switch (msg.what) {
                case 6:
                    adapter = new FragmentAdapter(getChildFragmentManager(), findClassifyManager.getFindClassify());
                    find_viewpager.setAdapter(adapter);
                    find_tab.setViewPager(find_viewpager);
                    break;
            }
        }
    }

}
